﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using eLoje_Model;
using Eloje_Data;
namespace ELoje_web
{
    public partial class popupFornecedor : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                navigationHistory();
                applyText();
                loadGrid();
            }
        }

        protected override void Render(HtmlTextWriter writer)
        {
            if (gvClientes.Rows.Count > 0)
            {
                gvClientes.HeaderRow.Cells[1].Attributes.Add("onclickk", ClientScript.GetPostBackEventReference(gvClientes, "Sort$COD_FORNECEDOR"));
                gvClientes.HeaderRow.Cells[1].Style.Add("cursor", "pointer");
                gvClientes.HeaderRow.Cells[1].Attributes.Add("onclickk", ClientScript.GetPostBackEventReference(gvClientes, "Sort$DESC_FORNECEDOR"));
                gvClientes.HeaderRow.Cells[1].Style.Add("cursor", "pointer");

                ClientScript.RegisterForEventValidation(gvClientes.UniqueID, "Sort$COD_FORNECEDOR");
                ClientScript.RegisterForEventValidation(gvClientes.UniqueID, "Sort$DESC_FORNECEDOR");
            }
            base.Render(writer);
        }

        protected void gvClientes_Sorting(object sender, GridViewSortEventArgs e)
        {
            List<FornecedorVO> ls = buscaLista();

            if (hdnShortExpression.Value == e.SortExpression)
            {
                if(hdnSortDirection.Value == "ASC")
                    gvClientes.DataSource = ls.OrderBy(x=> x.GetType().GetProperty(e.SortExpression).GetValue(x,null)).ToList();
                else
                    gvClientes.DataSource = ls.OrderByDescending(x => x.GetType().GetProperty(e.SortExpression).GetValue(x, null)).ToList();

                if (hdnSortDirection.Value == "ASC")
                    hdnSortDirection.Value = "DESC";
                else
                    hdnSortDirection.Value = "ASC";
            }
            else
            {
                gvClientes.DataSource = ls.OrderBy(x => x.GetType().GetProperty(e.SortExpression).GetValue(x, null)).ToList();
                hdnSortDirection.Value = "DESC";
            }
            hdnShortExpression.Value = e.SortExpression;
            gvClientes.DataBind();

        }

        protected void gvClientes_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Header)
            {
                System.Web.UI.WebControls.Image sortImg = new System.Web.UI.WebControls.Image();
                if (hdnSortDirection.Value == "ASC")
                    sortImg.ImageUrl = "~/img/SetaDown.png";
                else
                    sortImg.ImageUrl = "~/img/SetaUp.png";

                //if (hdnShortExpression.Value == "COD_FORNECEDOR" || hdnShortExpression.Value == "")
                //    e.Row.Cells[1].FindControl("divQuebraLinha").Controls.Add(sortImg);
                //if (hdnShortExpression.Value == "DES_FORNECEDOR")
                //    e.Row.Cells[2].FindControl("divQuebraLinha").Controls.Add(sortImg);
            }
        }

        protected void gvClientes_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            List<FornecedorVO> ls = buscaLista();
            if (hdnSortDirection.Value == "DESC")
                gvClientes.DataSource = ls.OrderBy(x => x.GetType().GetProperty(hdnShortExpression.Value).GetValue(x,null)).ToList();
            else
                gvClientes.DataSource = ls.OrderByDescending(x => x.GetType().GetProperty(hdnShortExpression.Value).GetValue(x, null)).ToList();
            gvClientes.PageIndex = e.NewPageIndex;
            gvClientes.DataBind();
        }

        protected void btnStart_Click(object sender, EventArgs e)
        {
            hdnStart.Value = txtDESC.Text;
            loadGrid();
        }
        private void navigationHistory()
        {
            lblThisPage.Text = " Lista de Fornecedores";
        }

        private void applyText()
        {
            btnCancel.Text = "Cancelar";
            btnStart.Text = "Iniciar em";
            btnSelect.Text = "Selecionar";
        }

        private void loadGrid()
        {
            List<FornecedorVO> ls = buscaLista();
            gvClientes.DataSource = ls;//.OrderBy(x => x.COD_FORNECEDOR).ToList();
            gvClientes.DataBind();
        }

        private List<FornecedorVO> buscaLista()
        {
            FornecedorDA da = new FornecedorDA();

            return da.getFornecedores();
        }
    }
}